package com.sixmac.common.base;

import com.sixmac.core.base.controller.BladeController;
import com.sixmac.core.constant.ConstCache;
import com.sixmac.core.constant.ConstCacheKey;
import com.sixmac.core.constant.ConstCurd;
import com.sixmac.core.toolbox.kit.StrKit;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

/**
 * 用于拓展controller类
 */
public class BaseController extends BladeController implements ConstCurd, ConstCache, ConstCacheKey {

    /**
     * 获取登录用户的IP
     *
     * @throws Exception
     */
    protected String getRemortIP() {
        String ip = "";
        if (getRequest().getHeader("x-forwarded-for") == null) {
            ip = getRequest().getRemoteAddr();
        } else {
            ip = getRequest().getHeader("x-forwarded-for");
        }
        return ip;
    }

    public HashMap<String, Object> getData(String key, Object val) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put(key, val);
        return map;
    }

    public String getParameter(String name, String defaultValue) {
        String result = getRequest().getParameter(name);
        return StrKit.notBlank(result) ? result : defaultValue;
    }

    @InitBinder
    protected void init(HttpServletRequest request, ServletRequestDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
    }

    /**
     * 文件下载
     *
     * @author gaolei
     */
    public static void download(HttpServletRequest request, HttpServletResponse response, String exportName, String filePath) throws Exception {
        // 解决文件名乱码问题
        if (request.getHeader("User-Agent").toUpperCase().indexOf("MSIE") > 0) { // IE浏览器
            exportName = URLEncoder.encode(exportName, "UTF-8");
        } else { // 其他浏览器
            exportName = new String(exportName.getBytes("UTF-8"), "ISO8859-1");
        }

        InputStream inStream;
        inStream = new FileInputStream(filePath);
        // 设置输出的格式
        response.setContentType("application/x-download");// 设置为下载application/x-download
        response.addHeader("content-type ", "application/x-msdownload");
        response.setContentType("application/octet-stream");
        // 设置输出的文件名
        response.setHeader("Content-Disposition", "attachment; filename=\"" + exportName + "\"");
        // 循环取出流中的数据
        byte[] b = new byte[100];
        int len;
        OutputStream outStream = response.getOutputStream();
        PrintStream out = new PrintStream(outStream);

        while ((len = inStream.read(b)) > 0) {
            out.write(b, 0, len);
            out.flush();
        }
        out.close();
        inStream.close();

        // 导出完成，删除文件
        File file = new File(filePath);
        file.delete();
    }
}
